﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.ADF.Connection;
using ESRI.ArcGIS.ADF.IMS;


public partial class WebServices_getVectorMap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string resultUrl = string.Empty;
        string strResultXml = string.Empty;

        string ServiceName = Request.QueryString[0].ToString();//地图服务名
        string mapTypeFlag = Request.QueryString[1].ToString();//地图服务请求类型（map和legend）
        string ExtentBox = Request.QueryString[2].ToString();//地图的地理范围
        string imgWith = Request.QueryString[3].ToString();//地图的图片宽度
        string imgHeight = Request.QueryString[4].ToString();//地图的图片高度
        string[] ExtentArray = ExtentBox.Split(',');
        string mapLeft = ExtentArray[0];
        string mapRight = ExtentArray[1];
        string mapTop = ExtentArray[2];
        string mapBottom = ExtentArray[3];
        string mapBox = mapLeft + "," + mapTop + "," + mapTop + "," + mapBottom;


        #region 获取常用的系统变量
        //string javaHome = System.Configuration.ConfigurationManager.AppSettings["JAVA_HOME"]; //Environment.GetEnvironmentVariable("JAVA_HOME", System.EnvironmentVariableTarget.Machine);
        string arcimsServer = System.Configuration.ConfigurationManager.AppSettings["ARCIMS_SERVER"];
        //string arcimsAdmin = System.Configuration.ConfigurationManager.AppSettings["ARCIMS_ADMIN"];
        //string arcimsPwd = System.Configuration.ConfigurationManager.AppSettings["ARCIMS_PWD"];
        //string arcimsServerInternet = System.Configuration.ConfigurationManager.AppSettings["ARCIMS_SERVER_INTERNET"];
        //string arcimsServerIntranet = System.Configuration.ConfigurationManager.AppSettings["ARCIMS_SERVER_INTRANET"];
        string imageUrl = "http://" + arcimsServer + "//output//";
        //string imageUrlIntranet = "http://" + arcimsServerIntranet + "//output//";
        //string imageUrlInternet = "http://221.208.13.140//output//";
        #endregion

        try
        {

            string XmlString = "";
            XmlString += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            XmlString += "\n" + "<ARCXML version=\"1.1\">";
            XmlString += "\n" + "<REQUEST>";
            XmlString += "\n" + "<GET_IMAGE>";
            XmlString += "\n" + "<PROPERTIES>";
            XmlString += "\n" + "<IMAGESIZE width=\"" + imgWith + "\" height=\"" + imgHeight + "\"/>";
            XmlString += "\n" + "<ENVELOPE minx=\"" + mapLeft + "\" miny=\"" + mapTop + "\" maxx=\"" + mapRight + "\" maxy=\"" + mapBottom + "\" />";
            if (mapTypeFlag == "IMSLEGEND")
            {
                XmlString += "\n" + "<LEGEND display=\"true\" width=\"240\" autoextend=\"true\" title=\"\" font=\"黑体\" titlefontsize=\"14\" titlefontstyle=\"bold\" layerfontsize=\"12\" layerfontstyle=\"bold\" valuefontsize=\"10\" cellspacing=\"8\" backgroundcolor=\"255,255,255\" />";
                XmlString += "\n" + "<DRAW map=\"false\" />";
            }
            else
            {
                XmlString += "\n" + "<LEGEND display=\"false\" />";
            }
            XmlString += "\n" + "</PROPERTIES>";
            XmlString += "\n" + "</GET_IMAGE>";
            XmlString += "\n" + "</REQUEST>";
            XmlString += "\n" + "</ARCXML>";




            ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection pCon = new ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection(arcimsServer, 5300);
            pCon.ServiceName = ServiceName;
            IMSResponse pImsResponse = new IMSResponse();

            if (mapTypeFlag == "IMSLEGEND")
            {
                resultUrl = pImsResponse.GetImageResponse(XmlString, pCon, "legend");
                //if (resultUrl.Length > 0)
                //{
                //    string pic_type = "_legend.jpg";
                //    //PicInformation.savaPicture(resultUrl, u_id, pic_type);//保存分级图到文件夹   
                //}
            }
            else
            {
                resultUrl = pImsResponse.GetImageResponse(XmlString, pCon, "map");
                //if (resultUrl.Length > 0)
                //{
                //    string pic_type = "_map.jpg";
                //    //PicInformation.savaPicture(resultUrl, u_id, pic_type);//保存分级图到文件夹 
                //    //PicInformation.createImgXml(u_id, DistName, season_name, _variety_name, module);//创建分级图信息的xml文件到文件夹
                //}
            }



            if (Request.Url.Host.ToLower().IndexOf("221") > -1)
            {
                //resultUrl = resultUrl.Replace(arcimsServer, arcimsServerInternet);
                resultUrl = resultUrl.Replace(arcimsServer, "221.208.13.140");

            }
            //else
            //{
            //    resultUrl = resultUrl.Replace(imageUrl, imageUrlIntranet);
            //}

            strResultXml += "<URLs>" + "\n";
            strResultXml += "<URL>" + resultUrl + "</URL> \n";
            if (mapBox != "")
            {
                strResultXml += "<ENVELOPE>" + mapBox + "</ENVELOPE> \n";//12-4日
            }
            strResultXml += "</URLs>" + "\n";
        }
        catch (Exception ex)
        {
            Log.WriteException(DateTime.Now.ToString() + ":" + ex.Message + "\n");
        }
        Response.Write(strResultXml);



    }
}
